<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="小区" prop="communityId">
            <communitySelect v-model="queryParams.communityId" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="房间号" prop="roomNum">
            <el-input
              v-model="queryParams.roomNum"
              placeholder="请输入房间号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="电话" prop="phone">
            <el-input
              v-model="queryParams.phone"
              placeholder="请输入电话"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择状态"
              clearable
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="repairList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="小区名称" align="center" prop="communityName">
        <template #default="scope">
          {{ matchCommunity(scope.row.communityId).name }}
        </template>
      </el-table-column>
      <el-table-column label="房间号" align="center" prop="roomNum" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column
        label="内容"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          {{ statusMap[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="维修类型" align="center" prop="type">
        <template #default="scope">
          {{ typeMap[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center" prop="type">
        <template #default="scope">
          {{ feeTypeMap[scope.row.feeType] }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template #default="scope">
          <el-button
            type="primary"
            v-if="[1, 3, 4].includes(scope.row.status)"
            link
            @click="handleEdit(scope.row)"
            >处理</el-button
          >
          <el-button type="primary" link @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <add-edit
      v-model="addEditDialog"
      v-if="addEditDialog"
      :row="curRow"
      @submit="getList"
    />
  </div>
</template>

<script setup name="Repair">
import { ref, getCurrentInstance } from "vue";
import { getRepairPage, removeRepairOrder } from "@/api/operateManage";
import communitySelect from "@/components/communitySelect/index.vue";
import { ElMessageBox, ElMessage } from "element-plus";
import addEdit from "./addEdit.vue";
import { matchCommunity } from "@/common/match";
import { feeTypeMap, typeMap, statusMap, statusOptions } from "./config";
const { proxy } = getCurrentInstance();
const repairList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const addEditDialog = ref(false);
const curRow = ref({});

const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  communityId: "",
  roomNum: "",
  status: null,
});

/** 查询报修列表 */
function getList() {
  loading.value = true;
  getRepairPage(queryParams.value).then(({ data }) => {
    repairList.value = data.records;
    total.value = data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  // 实现新增逻辑
}

/** 编辑按钮操作 */
function handleEdit(row) {
  curRow.value = row;
  addEditDialog.value = true;
}

// /** 删除按钮操作 */
function handleDelete(row) {
  ElMessageBox.confirm("是否确认删除该报修记录?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    removeRepairOrder({ id: row.id }).then(() => {
      getList();
      ElMessage.success("删除成功");
    });
  });
}

getList();
</script>
